package com.item.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.UserAddress;
import com.item.mall.mapper.UserAddressMapper;
import com.item.mall.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Autowired
    UserAddressMapper userAddressMapper;

    @Override
    public void setDefault(Integer userId, Integer id) {
        QueryWrapper queryWrapper =new QueryWrapper();
        UserAddress userAddress =new UserAddress();
        userAddress.setIsdefault(0);
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("isdefault",1);
        userAddressMapper.update(userAddress,queryWrapper);
        userAddress=new UserAddress();
        userAddress.setIsdefault(1);
        userAddress.setId(id);
        userAddressMapper.updateById(userAddress);
    }

    @Override
    public UserAddress getDefaultAddress(Integer userId) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("isdefault",1);
        return userAddressMapper.selectOne(queryWrapper);
    }


}
